﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sysgeprava.BL;
using Sysgeprava.Entities;
using Microsoft.Practices.Unity;

namespace Sysgeprava.Web.Controllers.Mantenimiento
{
    public class SexoController : Controller
    {
        [Dependency]
        public ISexoBL sexoBL { get; set; }
        //
        // GET: /Sexo/

        public ActionResult Index()
        {
            var lista = sexoBL.GetAllFromSexo();
            return View(lista);
        }

        [HttpPost]
        public ActionResult Index(string criterio = "")
        {
            var lista = sexoBL.GetAllFromSexoByCriterio(criterio);
            return View(lista);
        }

        public ActionResult Create()
        {
            var sexo = new SEXO();
            return View(sexo);
        }

        [HttpPost]
        public ActionResult Create(SEXO _sexo)
        {
            var sexo = new SEXO();

            try
            {
                sexo.Descripcion = _sexo.Descripcion.ToUpper();
                sexoBL.AddSexo(sexo);

                //TempData["AlertMessage"] = "Registro Correcto";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                msg = "Sexo ya Registrado";
                TempData["AlertMessage"] = msg;
                return RedirectToAction("Create");
            }
        }

        public ActionResult Edit(Int32 id)
        {
            var sexo = sexoBL.GetAllFRomSexoById(id);
            return View(sexo);
        }

        [HttpPost]
        public ActionResult Edit(SEXO _sexo)
        {
            var sexo = new SEXO();
            try
            {
                sexo.Id = _sexo.Id;
                sexo.Descripcion = _sexo.Descripcion.ToUpper();
                sexoBL.UpdateSexo(sexo);

                //TempData["AlertMessage"] = "Edición Correcta";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                msg = "Sexo ya Registrado";
                TempData["AlertMessage"] = msg;
                return RedirectToAction("Edit");
            }
        }

        public ActionResult Delete(Int32 id)
        {
            sexoBL.DeleteSexo(id);
            return RedirectToAction("Index");
        }

    }
}
